package cn.xorange.commons.data.service;

import cn.xorange.commons.data.build.TableBuild;
import cn.xorange.commons.data.core.entity.BaseEntity;
import cn.xorange.commons.data.core.entity.TreeEntity;
import cn.xorange.commons.data.entity.TableColumn;
import cn.xorange.commons.data.mapper.DaoHelperMapper;
import cn.xorange.commons.data.utils.DataUtil;
import cn.xorange.commons.data.utils.ParamUtil;
import cn.xorange.commons.exception.DataException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;

/**
 * @author : yangjian
 * @date : 2021/8/7
 * @description :  处理CRUD内部的sql服务
 */
@Component
public class DaoHelperService {

    @Resource
    private DaoHelperMapper daoHelperMapper;

    public List<TableColumn> getTableColumn(String dbType, String tableName){
        List<TableColumn> lst = this.daoHelperMapper.tableColumns(dbType,tableName);
        for (TableColumn o : lst) {
            o.setColName(o.getColName().toLowerCase());
            String dataType = o.getDataType().toLowerCase();
            switch (dataType) {
                case "bigint":
                    dataType = "long";
                    break;
                case "number":
                    if (o.getLength() > 16) {
                        dataType = "long";
                    } else {
                        dataType = "int";
                    }
                    break;
                case "int":
                    dataType = "int";
                    break;
                case "datetime":
                case "date":
                    dataType = "java.util.Date";
                    break;
                case "varchar":
                case "varchar2":
                case "nvarchar":
                case "nvarchar2":
                case "char":
                case "nchar":
                    dataType = "java.lang.String";
                    break;
                default:
                    break;

            }
            o.setDataType(DataUtil.getJdbcType(dataType));
        }
        return lst;
    }

}
